// Code Behind for usermanager.mxml
	import flash.events.Event;
	
	import jcrweb.as3.FlexUtil;
	import jcrweb.as3.LanguageUtil;
	import jcrweb.as3.RequestUtil;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
//	[Bindable]
//	private var rootDevice:XMLList;
	[Bindable]
	private var loginName:String;	
	[Bindable]
	private var usermanagedata:XMLListCollection;
	[Bindable]
	private var languageUtil:LanguageUtil;
	[Bindable]
	[Embed(source='../WebContent/images/attent.gif')]
	private var iconAlert:Class;
	[Bindable]
	[Embed(source='../WebContent/images/error.gif')]
	private var iconError:Class;
	
	private var curpage:String = "1";
	private var prepage:String = "1";
	private var nextpage:String = "1";
	private var allpage:String = "1";
	
//	private function loginCheck():void{
//		var loginUtil:LoginUtil = new LoginUtil();
//		loginUtil.loginCheck();
//	}
	// Event handler to initialize the MenuBar control.
//	private function init():void {
//		requestUserData();
//		userGrid.editable = true;
//	}
	private function requestUserData():void{
		languageUtil = LanguageUtil.getInstance();
		RequestUtil.sendRequest(RequestUtil.USERLIST_URL+"&page=1",Event.COMPLETE,loadUserData);
	}
	
	private function previousUserData():void{
		languageUtil = LanguageUtil.getInstance();
		RequestUtil.sendRequest(RequestUtil.USERLIST_URL+"&page="+prepage,Event.COMPLETE,loadUserData);
	}
	private function nextUserData():void{
		languageUtil = LanguageUtil.getInstance();
		RequestUtil.sendRequest(RequestUtil.USERLIST_URL+"&page="+nextpage,Event.COMPLETE,loadUserData);
	}
	private function loadUserData(event:Event):void{       
		usermanagedata = new XMLListCollection();
		var loader:URLLoader = URLLoader(event.target); 
		var resutlSize:String = FlexUtil.getTag(loader.data,FlexUtil.RESULT_SIZE);
		var allData:String = FlexUtil.getTag(loader.data,FlexUtil.RESULT_DATA)
		if(resutlSize != "0"){
			
			curpage = FlexUtil.getTag(allData,"curpage");
			prepage = FlexUtil.getTag(allData,"prepage");
			nextpage = FlexUtil.getTag(allData,"nextpage");
			allpage = FlexUtil.getTag(allData,"allpage");
			
			pageMes.text = curpage+" / "+allpage;
			
			for(var j:int=0;j<Number(resutlSize);j++){
				try{
					var newnode:XML = new XML(FlexUtil.getTag(allData,new String(j+1)));
					usermanagedata.addItem(newnode);
				}catch(e:Error){} 
			}
		}
	}
	private function closeDialog():void{
		PopUpManager.removePopUp(this);
	}
	private function reFresh(event:Event=null):void{
		requestUserData();
	}
	private function createUser():void{
		var myPanel:createuser = createuser(PopUpManager.createPopUp(this, createuser, true));
		myPanel.addEventListener(Event.REMOVED,reFresh);
		PopUpManager.centerPopUp(myPanel);
	}
	private function saveEditUser():void{
		if(userGrid.selectedItem == null){
			Alert.show("Please select a record.","Attention",0,null,null,iconAlert);
			return;
		}
		var userXml:XML = userGrid.selectedItem as XML;
		if(userXml.userName == ""){
			Alert.show("please input name.","Error",0,null,null,iconError);
			return;
		}
		if(userXml.password == ""){
			Alert.show("please input password.","Error",0,null,null,iconError);
			return;
		}
		var para:String = new String();
		para +="&userName="+userXml.userName;
		para +="&userCode="+userXml.userCode;
		para +="&password="+userXml.password;
		para +="&datetime="+userXml.datatime;
		para +="&perpath="+userXml.permissionDirPath;
		para +="&mail="+userXml.mail;
		para +="&note="+userXml.note;
		para =  encodeURI(para);
		para =  encodeURI(para);
		
		var url:String = RequestUtil.UPDATEUSER_URL+para;
		RequestUtil.sendRequest(url,Event.COMPLETE,finish);
	}
	private function deleteUser():void{
		if(userGrid.selectedItem == null){
				Alert.show("Please select a user.","Attention",0,null,null,iconAlert);
				return;
		}
		Alert.show("Do you really want to delete this user?","Attention",3,this,deleteInfo,iconAlert);
	}
	private function deleteInfo(event:CloseEvent):void{
		if(event.detail == Alert.YES){
			var userXml:XML = userGrid.selectedItem as XML;
			var para:String = new String();
			para +="&userCode="+userXml.userCode;
			para =  encodeURI(para);
			para =  encodeURI(para);
				
			var url:String = RequestUtil.DELETEUSER_URL+para;
			RequestUtil.sendRequest(url,Event.COMPLETE,finish);
		}
	}
	public function finish(event:Event):void {
		var loader:URLLoader = URLLoader(event.target); 
		var isError:String = FlexUtil.getTag(loader.data,FlexUtil.IS_ERROR);
		if(isError == "true" ){
			var errorMes:String = FlexUtil.getTag(loader.data,FlexUtil.ERROR_MES_TAG);
			Alert.show("Faild! "+errorMes,"Error");
		}
		requestUserData();
	}
	private function showPermissionset():void{
		if(userGrid.selectedItem == null){
			Alert.show("Please select a record.","Attention",0,null,null,iconAlert);
			return;
		}
		var userXml:XML = userGrid.selectedItem as XML;
		var myPanel:permissionset = permissionset(PopUpManager.createPopUp(this, permissionset, true));
		myPanel.input_usercode.text = userXml.userCode;
		myPanel.input_username.text = userXml.userName;
		myPanel.input_perdir.text = userXml.permissionDirPath;
		
		myPanel.setReturnobj(userXml);
		PopUpManager.centerPopUp(myPanel);
	}
	private function keyDownEvent(keyCode:uint):void{
			if(keyCode == 27){
				this.closeDialog();
			}
	}
